package form.ui
{
	import flash.display.DisplayObject;
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flash.utils.getTimer;

	/** 
	 * The Effect class represents a generic effect with a predefined time interval.
	 */	
	public class Effect extends EventDispatcher {
		private var _target:DisplayObject;
		
		protected static const INTERVAL:uint = 30;
		protected var timer:Timer = new Timer(Effect.INTERVAL);
		protected var startTime:uint = 0;
		protected var lastTime:uint = 0;
		
		public function Effect() {
			super();
			
			this.timer.addEventListener(TimerEvent.TIMER, this.onTimer);
		}
		
		public function get target():DisplayObject { return this._target; }
		public function set target(object:DisplayObject):void { 
			this._target = object; 
			if (this.timer.running) { this.draw(); }
		}
		
		public function start():void {
			if (this.timer.running) { this.reset(); }
			
			this.startTime = this.lastTime = getTimer();
			timer.start();
			
			this.draw(0);
		}
		
		public function stop():void {
			if (this.timer.running) { this.timer.stop(); }
			this.startTime = this.lastTime = 0;
		}
		
		public function reset():void {
			this.stop();
			timer.reset();
		}
		
		protected function draw(interval:uint = 0):void {}
		
		protected function onTimer(event:TimerEvent):void {
			var current:uint = getTimer();
			var interval:uint = current - this.lastTime;
			this.lastTime = current;
			
			this.draw(interval);
		}
	}
}